import grails.test.*

class NewsTests extends GrailsUnitTestCase {
    protected void setUp() {
        super.setUp()
    }

    protected void tearDown() {
        super.tearDown()
    }
    
    void testParsingRank() {
    	String section = 'all'
    	String date = new Date().format('yyyyMMdd').toString()
    	
		Date dateParam = Date.parse('yyyyMMdd', date)
    	def rankInfo = RankInfo.findBySectionAndDateCreatedBetween(section, new Date(dateParam.time), new Date((dateParam+1).time))
		if(!rankInfo) {
			rankInfo = RankInfo.parseRankInfo(section, date)
			rankInfo.save()
		}
    	println "$section, $date"
    	
		def sortedListOfArticleRank = rankInfo.listOfArticleRank.sort{ it.rank }
		sortedListOfArticleRank.each{
			println "$it.rank, $it.articleTitle"
		}
    }
    
    // 기사 본문 파싱 테스트 
    void testParsingArticle() {
		String articleUrl = 'http://news.nate.com/view/20091203n08304'
		def article = Article.parseArticle(articleUrl)
    	
		println "$article.title($article.articleId, $article.url, $article.dateCreated, $article.dateModified, $article.media)"
		println "$article.imageLink, $article.thumbnailLink"
		println "$article.content"
		
		article.save()
    }
    
    // 기사 베플 파싱 테스트 
    void testParsingBestReplies() {
		String articleUrl = 'http://news.nate.com/view/20091203n08304'
		def listOfBeple = Reply.parseBestReplies(articleUrl)
		
		for(beple in listOfBeple) {
			println "$beple.username, $beple.content, $beple.dateCreated"
			
			beple.save()
		}
    	
		assert Reply.count() > 0
    }

    // 기사 본문에 베플을 넣어 저장하고 잘 가져와지는지 테스트 
    void testArticleBestReplies() {
    	String articleUrl = 'http://news.nate.com/view/20091203n08304'
    	def article = Article.parseArticle(articleUrl)
		article.listOfBeple = Reply.parseBestReplies(articleUrl)
		
		article.save()
		
		def insertedArticle = Article.findByUrl(articleUrl)
		assert insertedArticle
		
		println insertedArticle
		insertedArticle.listOfBeple.sort{ it.countOfGood }.reverse().each{ beple -> println beple }
    }
}
